package cn.rongdao.jrkabao.util;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import cn.rongdao.jrkabao.ui.loan.LoanCommitActivity;
import cn.rongdao.jrkabao.ui.loan.adapter.CreditLoanMaterialAdapter;

/**
 * Created by Wang on 17/9/20.
 */

public class CreditCardLoansAnimaManager {
   private TextView copy_textview;
   private LinearLayout materialLayout;
   private RecyclerView recyclerViewTab;
   private SeekBar seekBar;
   private TextView amount_min;
   private TextView amount_max;
   private TextView loans_amount;
   private ImageView img_dowm;
   private GridLayoutManager gridLayoutManager;
   private CreditLoanMaterialAdapter materialAdapter;
   private View bg_view;
   private TextView termText;
   private LinearLayout cusLinearLayout;

    public void setCusLinearLayout(LinearLayout cusLinearLayout) {
        this.cusLinearLayout = cusLinearLayout;
    }

    public void setTermText(TextView termText) {
        this.termText = termText;
    }

    public TextView getCopy_textview() {
        return copy_textview;
    }

    public void setCopy_textview(TextView copy_textview) {
        this.copy_textview = copy_textview;
    }

    public LinearLayout getMaterialLayout() {
        return materialLayout;
    }

    public void setMaterialLayout(LinearLayout materialLayout) {
        this.materialLayout = materialLayout;
    }

    public RecyclerView getRecyclerViewTab() {
        return recyclerViewTab;
    }

    public void setRecyclerViewTab(RecyclerView recyclerViewTab) {
        this.recyclerViewTab = recyclerViewTab;
    }

    public SeekBar getSeekBar() {
        return seekBar;
    }

    public void setSeekBar(SeekBar seekBar) {
        this.seekBar = seekBar;
    }

    public TextView getAmount_min() {
        return amount_min;
    }

    public void setAmount_min(TextView amount_min) {
        this.amount_min = amount_min;
    }

    public TextView getAmount_max() {
        return amount_max;
    }

    public void setAmount_max(TextView amount_max) {
        this.amount_max = amount_max;
    }

    public TextView getLoans_amount() {
        return loans_amount;
    }

    public void setLoans_amount(TextView loans_amount) {
        this.loans_amount = loans_amount;
    }

    public ImageView getImg_dowm() {
        return img_dowm;
    }

    public void setImg_dowm(ImageView img_dowm) {
        this.img_dowm = img_dowm;
    }

    public GridLayoutManager getGridLayoutManager() {
        return gridLayoutManager;
    }

    public void setGridLayoutManager(GridLayoutManager gridLayoutManager) {
        this.gridLayoutManager = gridLayoutManager;
    }

    public CreditLoanMaterialAdapter getMaterialAdapter() {
        return materialAdapter;
    }

    public void setMaterialAdapter(CreditLoanMaterialAdapter materialAdapter) {
        this.materialAdapter = materialAdapter;
    }

    public void setBg_view(View bg_view) {
        this.bg_view = bg_view;
    }

    /*位移动画*/
    public void tran(final boolean tag, final String from, View textView, int[] location1, int targetX, int targetY) {
        //tag = false:准备打开  tag = true:准备关闭
        int viewX = location1[0];
        int viewY =  location1[1];
        ObjectAnimator transXAnimator;
        ObjectAnimator transYAnimator;
        if (from.equals(LoanCommitActivity.FROM_DATE)) {
            copy_textview.setVisibility(View.VISIBLE);
        }
        if (!tag) {
            transXAnimator = ObjectAnimator.ofFloat(textView, "translationX", 0f, targetX - viewX);
            transYAnimator = ObjectAnimator.ofFloat(textView, "translationY", 0f, targetY - viewY);
        } else {
            transXAnimator = ObjectAnimator.ofFloat(textView, "translationX", targetX - viewX, 0f);
            transYAnimator = ObjectAnimator.ofFloat(textView, "translationY", targetY - viewY, 0f);
        }

        AnimatorSet set = new AnimatorSet();
        set.play(transXAnimator).with(transYAnimator);
        set.setDuration(200);
        set.setInterpolator(new LinearInterpolator());
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                materialLayout.setEnabled(false);
                bg_view.setEnabled(false);
                cusLinearLayout.setEnabled(false);
                //这里不能设置GONE 这些布局的位置不能没有占位的布局 要不然 执行动画的View的位置就会乱掉
                if(recyclerViewTab.getVisibility()== View.VISIBLE) {
                    recyclerViewTab.setVisibility(View.INVISIBLE);
                    seekBar.setVisibility(View.INVISIBLE);
                    amount_min.setVisibility(View.INVISIBLE);
                    amount_max.setVisibility(View.INVISIBLE);
                }
                loans_amount.setTextSize(TypedValue.COMPLEX_UNIT_SP,30);

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //回来的动画
                if (from.equals(LoanCommitActivity.FROM_DATE)) {
                    //是选择日期期限的时候做这些操作
                    if(tag) {
                        copy_textview.setVisibility(View.GONE);
                    }
                }
                if(tag){
                    if(recyclerViewTab.getVisibility() == View.INVISIBLE) {
                        recyclerViewTab.setVisibility(View.VISIBLE);
                    }
                    if(img_dowm.getVisibility() == View.VISIBLE) {
                        img_dowm.setVisibility(View.GONE);
                    }
                    seekBar.setVisibility(View.VISIBLE);
                    amount_min.setVisibility(View.VISIBLE);
                    amount_max.setVisibility(View.VISIBLE);
                    termText.setVisibility(View.VISIBLE);
                }else {
                    if(img_dowm.getVisibility() == View.GONE) {
                        img_dowm.setVisibility(View.VISIBLE);
                    }
                }
                if (from.equals(LoanCommitActivity.FROM_RMB)) {
                    if(tag){
                        loans_amount.setTextSize(TypedValue.COMPLEX_UNIT_SP,34);
                    }
                }
                materialLayout.setEnabled(true);
                bg_view.setEnabled(true);
                cusLinearLayout.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        set.start();
    }
   /*列表的为移动动画*/
    public void performAnim2(boolean show,int targetHeight,int originalHeight){

        //View是否显示的标志
        show = !show;
        //属性动画对象
        ValueAnimator va ;
        if(show){
            //显示view，targetHeight
            va = ValueAnimator.ofInt(originalHeight,targetHeight);
        }else{
            //隐藏view，高度从targetHeight变为originalHeight
            va = ValueAnimator.ofInt(targetHeight,originalHeight);
        }
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //获取当前的height值
                int h =(Integer)valueAnimator.getAnimatedValue();
                //动态更新view的高度materialLayout.getLayoutParams().height = h;
                materialLayout.getLayoutParams().height = h;
                materialLayout.requestLayout();
            }
        });
        va.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                materialLayout.setEnabled(false);
                bg_view.setEnabled(false);
                cusLinearLayout.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (gridLayoutManager.getSpanCount() != CreditLoanMaterialAdapter.SPAN_COUNT_ONE) {
                    gridLayoutManager.setSpanCount(CreditLoanMaterialAdapter.SPAN_COUNT_ONE);
                } else {
                    gridLayoutManager.setSpanCount(CreditLoanMaterialAdapter.SPAN_COUNT_OTHER);
                }
                materialAdapter.notifyItemRangeChanged(0, materialAdapter.getItemCount());
                materialLayout.setEnabled(true);
                bg_view.setEnabled(true);
                cusLinearLayout.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        va.setDuration(200);
        //开始动画
        va.start();
    }
    /*背景图的位移动画*/
    public void bgViewAnim2(boolean show, int targetHeight, int originalHeight){
        //View是否显示的标志
        //属性动画对象
        ValueAnimator va ;
        if(show){
            //显示view，targetHeight
            va = ValueAnimator.ofInt(originalHeight,targetHeight);
        }else{
            //隐藏view，高度从targetHeight变为originalHeight
            va = ValueAnimator.ofInt(targetHeight,originalHeight);
        }
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //获取当前的height值
                int h =(Integer)valueAnimator.getAnimatedValue();
                //动态更新view的高度materialLayout.getLayoutParams().height = h;
                bg_view.getLayoutParams().height = h;
                bg_view.requestLayout();
            }
        });
        va.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                materialLayout.setEnabled(false);
                bg_view.setEnabled(false);
                cusLinearLayout.setEnabled(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                bg_view.setEnabled(true);
                materialLayout.setEnabled(true);
                cusLinearLayout.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        va.setDuration(200);
        //开始动画
        va.start();
    }

    /*背景图的位移动画*/
    public void bgViewAlphaAnim2(boolean show){
        //View是否显示的标志
        //属性动画对象
        final ValueAnimator va ;
        if(img_dowm.getVisibility() == View.GONE){
            img_dowm.setVisibility(View.VISIBLE);
            img_dowm.setAlpha(0);
        }
        if(show){
            va = ValueAnimator.ofFloat(255,0);
        }else{
            va = ValueAnimator.ofFloat(0,255);
        }
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float h = (float) valueAnimator.getAnimatedValue();
                img_dowm.setAlpha(h);
            }
        });
        va.setDuration(200);
        //开始动画
        va.start();
    }
}
